<?php
class PageAction extends BaseAction
{
    function index()
    {
        $map = "1=1 ";
        if($_GET['nid']) $map .= "and nid=".$_GET['nid'];
        $Dao = D("Page");

        $count = $Dao->count($map);
        import("ORG.Util.Page");
        $listRows = 20;
        $p = new Page($count,$listRows);

        $field = "*";
        $list = $Dao->findAll($map,$field,"id desc",$p->firstRow.",".$p->listRows);
        //dump($Dao->getLastSql());
        $page = $p->show();
        $this->assign("list",$list);
        $this->assign("page",$page);
        $this->display();
    }

    function add()
    {
        // 添加内容的页面
        $Dao = D("Sysnav");
        $list = $Dao->where("modelName='page'")->order('orderNum asc')->findAll();
        $list = $Dao->toTree($list,'id','fid','_child');
        $this->treetoary($list);
        $this->flist = $this->tree;

        $this->display();
    }

    function insert()
    {
        // 保存添加的数据
        $Dao = D("Page");
        $vo = $Dao->create();
        if(false === $vo) {
            $this->error($Dao->getError());
        }
        $Dao->content = h($Dao->content);
        $rs = $Dao->add();
        if($rs){
            $this->assign("jumpUrl",__URL__."/index/nid/".$_POST['nid']);
            $this->success("添加数据成功!");
        }else{
            $this->error("添加数据失败");
        }
    }

    function edit()
    {
        // 编辑页面
        $Dao = D("Page");
        $vo = $Dao->find("id=".$_GET["id"]);
        if($vo === false){
            $this->assign("jumpUrl",__URL__."/nid/".$vo['nid']);
            $this->error("Non-existed record");
        }
//dump($vo);
        $this->assign("vo",$vo);
        $this->display();
    }

    function update()
    {
        // 保存编辑过的数据
        $Dao = D("Page");
        $vo = $Dao->create("","edit");
        if(false === $vo) {
            $this->error($Dao->getError());
        }
        $Dao->content = h($Dao->content);
        $rs = $Dao->save();
        if($rs){
            $this->assign("jumpUrl",__URL__);
            $this->success("保存修改成功!");
        }else{
            $this->error("保存修改失败");
        }
    }

    function delete()
    {
        // 根据id删除指定的记录
        $Dao = D("Page");
        $id = $_REQUEST["id"];
        if(isset($id)) {
            $result = $Dao->getById($id);
            if(!$result) {
                $this->error("Non-existed record!");
            }
            if($Dao->deleteById($id)){
                $this->assign("jumpUrl",__URL__."/index/nid/".$_GET['nid']);
                $this->success("删除数据成功");
            }else {
                $this->error("删除数据失败");
            }
        }else {
            $this->error("非法操作");
        }
    }

    var $tree=array();
    function treetoary($list,$level=0) {
        foreach($list as $key=>$val)
        {
            $tmp_str=str_repeat("&nbsp;",$level*2);
            if($level>0) {
                $tmp_str.="├";
            }
            $val['name']=$tmp_str."&nbsp;".$val['name'];
            if(!array_key_exists('_child',$val)) {
                array_push($this->tree,$val);
            }else{
                $tmp_ary = $val['_child'];
                unset($val['_child']);
                array_push($this->tree,$val);
                $this->treetoary($tmp_ary,$level+1);
            }
        }
        return;
    }
}
?>